Inspired by https://github.com/rust-lang/cargo/pull/1182.
Fixes #1158, #1772, #1773.
use std::env;
use std::error::Error;
-use std::path::PathBuf;
use cargo::core::{Package, Source};
use cargo::util::{CliResult, CliError, Config};
env::args().collect::<Vec<_>>());
try!(config.shell().set_color_config(options.flag_color.as_ref().map(|s| &s[..])));
- // Accept paths to directories containing Cargo.toml for backwards compatibility.
- let root = match options.flag_manifest_path {
- Some(path) => {
- let mut path = PathBuf::from(path);
- if !path.ends_with("Cargo.toml") {
- path.push("Cargo.toml");
- }
- Some(path.display().to_string())
- },
- None => None,
- };
- let root = try!(find_root_manifest_for_cwd(root));
-
- debug!("read-manifest; manifest-path={}", root.display());
+ let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path));
let mut source = try!(PathSource::for_path(root.parent().unwrap(), config).map_err(|e| {
CliError::new(e.description(), 1)
use std::collections::HashMap;
+use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::process;
let mut contents = String::new();
let filename = args.flag_manifest_path.unwrap_or("Cargo.toml".into());
+
+ if !filename.ends_with("Cargo.toml") {
+ fail("invalid", "the manifest-path must be a path to a Cargo.toml file")
+ }
+ if !fs::metadata(&filename).is_ok() {
+ fail("invalid", &format!("manifest path `{}` does not exist", filename))
+ }
+
let file = File::open(&filename);
match file.and_then(|mut f| f.read_to_string(&mut contents)) {
Ok(_) => {},
human("Couldn't determine the current working directory")
}));
match manifest_path {
- Some(path) => Ok(cwd.join(&path)),
+ Some(path) => {
+ let absolute_path = cwd.join(&path);
+ if !absolute_path.ends_with("Cargo.toml") {
+ return Err(human("the manifest-path must be a path to a Cargo.toml file"))
+ }
+ if !fs::metadata(&absolute_path).is_ok() {
+ return Err(human(format!("manifest path `{}` does not exist", path)))
+ }
+ Ok(absolute_path)
+ },
None => find_project_manifest(&cwd, "Cargo.toml"),
}
}
assert_that(p.cargo_process("read-manifest")
.arg("--manifest-path").arg("foo")
.cwd(p.root().parent().unwrap()),
- execs().with_status(0)
- .with_stdout(read_manifest_output()));
+ execs().with_status(101)
+ .with_stderr("the manifest-path must be a path to a Cargo.toml file"));
});
test!(cargo_read_manifest_path_to_cargo_toml_parent_absolute {
assert_that(p.cargo_process("read-manifest")
.arg("--manifest-path").arg(p.root())
.cwd(p.root().parent().unwrap()),
- execs().with_status(0)
- .with_stdout(read_manifest_output()));
+ execs().with_status(101)
+ .with_stderr("the manifest-path must be a path to a Cargo.toml file"));
});
test!(cargo_read_manifest_cwd {